package com.example.demo5.info.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo5.info.mapper.LoginMapper;
import com.example.demo5.info.pojo.Login;
import com.example.demo5.info.service.LoginService;
import com.example.demo5.result.AjaxResult;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;
@Service
public class LoginServiceImpl extends ServiceImpl<LoginMapper, Login>implements LoginService {
    @Resource
    private  LoginMapper loginMapper;
    //注册的方法
    @Override
    public AjaxResult handleAdd(Login login) {
        String newName=login.getUserName();
        QueryWrapper<Login> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_name",newName);
        List<Login> lsits=loginMapper.selectList(queryWrapper);
        if(lsits.size()!=0){
            return AjaxResult.error("该账户已存在");
        }
        loginMapper.insert(login);
        return  AjaxResult.success("注册成功");
    }

    //  登录的方法
    @Override
    public AjaxResult handleOn(Login login) {
        String newName=login.getUserName();
        String newPass=login.getUserPass();
        QueryWrapper<Login> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_name",newName);
        List<Login> lsits=loginMapper.selectList(queryWrapper);
        if(lsits.size()==0){
            return AjaxResult.error("账户不存在");
        }
        if(!lsits.get(0).getUserPass().equals(newPass)){
            return AjaxResult.error("密码错误");
        }
        //生成token
        UUID uuid= UUID.randomUUID();
        String token = uuid.toString();
        return AjaxResult.success("登录成功").put("token",token).put("id",lsits.get(0).getId());
    }

    @Override
    public AjaxResult handeleGetId(long id) {
        QueryWrapper<Login> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("id",id);
        List<Login> lsits=loginMapper.selectList(queryWrapper);
        return  AjaxResult.success(lsits.get(0));
    }
}
